package com.wst.system.config.job;

import com.alibaba.fastjson2.JSONArray;
import com.wst.system.server.entity.SysJob;
import com.wst.system.util.ReflectUtils;
import com.wst.system.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

@Slf4j
public class AnnotationJob extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDetail jobDetail=jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap=jobDetail.getJobDataMap();
        SysJob job =(SysJob) jobDataMap.get("sysjob");
        try {
            Class<?> cls=Class.forName(job.getMethodclazz());
            Object obj=SpringContextUtils.getBean(cls);
            JSONArray jsonArray= JSONArray.parseArray(job.getParams());
            Object [] args=new Object[jsonArray.size()] ;
            for (int i = 0; i < jsonArray.size(); i++) {
                args[i]=jsonArray.get(i);
            }
            ReflectUtils.methodInvoke(obj,job.getMethodname(),args);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
